<?php

use app\model\CouponBrand;
use app\model\PageSetting;
use think\facade\Filesystem;
use think\migration\Migrator;

class AlterChangeBrandSettingDir extends Migrator
{
   public function up()
   {
       if(!isProd()) {
           echo '非生产环境不执行修改文件路径'.PHP_EOL;
           return;
       }
       $newImageFileArr = array();

       //查询Bucket名称
       $bucket = Filesystem::getDiskConfig('aliyun', 'bucket');

       //查询地域节点
       $endpoint = Filesystem::getDiskConfig('aliyun', 'endpoint');

       $baseUrl = 'https://'.$bucket.'.'.$endpoint;

       //品牌表image
       CouponBrand::query()->whereLike('image_url', '%dmjvip.com%')
           ->field('id, image_url')
           ->select()->each(function (CouponBrand $couponBrand) use($baseUrl, &$newImageFileArr) {
               if (!$couponBrand->isEmpty()) {
                   if (!empty($couponBrand->image_url)) {
                       //查询数据
                       $imageArr = explode('.com/', $couponBrand->image_url);
                       //数据替换
                       $couponBrand->image_url = $baseUrl.'/'.$imageArr[1];
                       array_push($newImageFileArr,$couponBrand->image_url);
                       $couponBrand->save();
                   }
               }
           });
       echo '品牌表image修改完成'.PHP_EOL;
       //icon
       CouponBrand::query()->whereLike('icon', '%dmjvip.com%')
           ->field('id, icon')
           ->select()->each(function (CouponBrand $couponBrand) use($baseUrl, &$newImageFileArr){
               if (!$couponBrand->isEmpty()) {
                   if (!empty($couponBrand->icon)) {
                       //查询数据
                       $iconArr = explode('.com/', $couponBrand->icon);
                       //数据替换
                       $couponBrand->icon = $baseUrl.'/'.$iconArr[1];
                       array_push($newImageFileArr,$couponBrand->icon);
                       $couponBrand->save();
                   }
               }
           });
       echo '品牌表icon修改完成'.PHP_EOL;
       //页面跳转配置
       PageSetting::query()->whereLike('image', '%dmjvip.com%')
           ->field('id, image')
           ->chunk(50, function ($list) use ($baseUrl, &$newImageFileArr) {
               if (empty($list)) {
                   return false;
               }
               /**
                * @var PageSetting $pageSetting
                */
               foreach ($list as $pageSetting) {
                   if ($pageSetting->isEmpty()) {
                       continue;
                   }
                   if (empty($pageSetting->image)) {
                       continue;
                   }
                   //数据备份
                   $pageSetting->old_image = $pageSetting->image;
                   //查询数据
                   $imageArr = explode('.com/', $pageSetting->image);
                   //数据替换
                   $pageSetting->image = $baseUrl.'/'.$imageArr[1];
                   array_push($newImageFileArr,$pageSetting->image);
                   $pageSetting->save();
               }
           });
       echo '页面跳转配置表image修改完成'.PHP_EOL;
       file_put_contents('new_dir.txt', var_export(array_values(array_unique($newImageFileArr)),true));
   }
}
